<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>注册</title>

    <!-- Bootstrap core CSS -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <!--<link href="signin.css" rel="stylesheet">-->
    <style type="text/css">
        body {
            padding-top: 140px;
            padding-bottom: 40px;
            background-color: #eee;
        }

        .form-signin {
            max-width: 330px;
            padding: 15px;
            margin: 0 auto;
        }

        .form-signin .form-signin-heading,
        .form-signin .checkbox {
            margin-bottom: 10px;
        }

        .form-signin .checkbox {
            font-weight: normal;
        }

        .form-signin .form-control {
            position: relative;
            height: auto;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            padding: 10px;
            font-size: 16px;
        }

        .form-signin .form-control:focus {
            z-index: 2;
        }

        .form-signin input[type="email"] {
            margin-bottom: -1px;
            border-bottom-right-radius: 0;
            border-bottom-left-radius: 0;
        }

        .form-signin input[type="password"] {
            margin-bottom: 10px;
            border-top-left-radius: 0;
            border-top-right-radius: 0;
        }
    </style>

    <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>

<div class="container">

    <form class="form-signin" th:action="@{/user/registerUser}" action="/user/registerUser" method="post">
        <h2 class="form-signin-heading" style="margin-bottom: 7px;display: inline">请注册</h2>
        <a type="button" class="btn" th:href="@{/user/toLoginPage}"
           style="margin-bottom: 5px;margin-left: 95px">返回登录页面</a>
        <br>

        <!--th:if="${not #strings.isEmpty(msg)}"-->
        <span id="message" th:text="${msg}" style="color: red"></span>
        <!--<span id="message"></span>-->
        <input type="text" id="username" name="username" class="form-control" placeholder="请输入您注册的用户名" required
               onblur="verifyUser()" autofocus minlength="5" maxlength="15" style="margin-bottom: 10px">
        <input type="password" id="password" name="password" class="form-control" placeholder="请输入您注册的密码" required
               onkeyup="value=value.replace(/[^\w\.\/]/ig,'')" minlength="5" maxlength="15" style="margin-bottom: 10px">
        <input type="email" id="email" name="useremail" class="form-control" placeholder="请输入您可接收邮件的邮箱" required>
        <br>
        <!--onclick="return confirm('您需要等待20秒左右的时间，确认要注册吗？')"-->
        <button id="button" class="btn btn-lg btn-primary btn-block" type="submit">注册</button>
    </form>
</div> <!-- /container -->
<script>
    function verifyUser() {
        $.ajax({
            type: "get",
            url:"/user/verityUser/"+$("#username").val(),
            // data:{
            //     username:$("#username").val()
            // },
            success(data){
                if (data=="ERROR"){
                    console.log("控制台打印的信息=>>用户名"+$("#username").val()+"已存在");
                    $("#message").text("用户名"+$("#username").val()+"已经存在");
                    /*当所填用户存在时,禁用点击按钮*/
                    /*这里增加了按钮的禁用, 在UserController方法中的exitUsername可以不执行,这里对其没有操作*/
                    $("#button").attr("disabled",true);
                } else {
                    // 这里应该不做反应，或是与if进行反向操作
                    $("#message").text("");
                    /*当所填用户存在时,解除禁用点击按钮*/
                    $("#button").attr("disabled",false);
                }
            },
            error(){
                console.log("ajax配置有错，请检查url与type是否正确");
            }
        });
    }
</script>
</body>
</html>